package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Game.*;
    import com.gengine.global.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.utils.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.rules.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.map.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.pet.*;
    import mortal.game.view.petMeet.*;
    import mortal.game.view.petMeet.info.*;
    import mortal.mvc.core.*;

    public class PetWareHouseController extends Controller
    {
        private var _petWareHouseModule:PetWareHouseModule;
        private var _petMeetSelectPetWindow:PetMeetSelectPetWindow;
        private var _petViewWindow:PetViewWindow;
        private var _petOrder:int = 1;
        private var _isStore:Boolean = false;
        private var _isTakeOut:Boolean = false;
        private var _timer:Timer;

        public function PetWareHouseController()
        {
            
        }

        public function get petWareHouseModule() : PetWareHouseModule
        {
            if (this._petWareHouseModule == null)
            {
                this._petWareHouseModule = new PetWareHouseModule();
            }
            return this._petWareHouseModule;
        }

        public function get petMeetSelectPetWindow() : PetMeetSelectPetWindow
        {
            if (this._petMeetSelectPetWindow == null)
            {
                this._petMeetSelectPetWindow = new PetMeetSelectPetWindow();
            }
            return this._petMeetSelectPetWindow;
        }

        public function get petViewWindow() : PetViewWindow
        {
            if (this._petViewWindow == null)
            {
                this._petViewWindow = new PetViewWindow();
            }
            return this._petViewWindow;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.PetStorageOpen, this.onPetStorageOpenHandler);
            Dispatcher.addEventListener(PetMeetEventName.ConfirmSelectedPet, this.onConfirmSelectedPetHandler);
            Dispatcher.addEventListener(EventName.PetWareHouse_LookUpPet, this.onLookUpPetHandler);
            Dispatcher.addEventListener(EventName.PetWareHouse_TakeOutPet, this.onTakeOutPetHandler);
            Dispatcher.addEventListener(EventName.PetWareHouse_StorePet, this.onStorePetHandler);
            Dispatcher.addEventListener(EventName.PetWareHouse_ExpendCapacity, this.onExpendCapacityHandler);
            Dispatcher.addEventListener(EventName.PetBagUpdate, this.onPetBagUpdateHandler);
            Dispatcher.addEventListener(EventName.PetWareHouse_CloseWindow, this.onWindowCloseHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateWarehousePetInfo, this.onRecivePetInfoHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetWareHouseCapacityChange, this.onBagCapacityChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetWareHousePetChange, this.onWareHousePetChangeHandler);
            
        }

        private function onPetStorageOpenHandler(event:DataEvent) : void
        {
            this.setPetOrder();
            var _loc_2:* = (Global.stage.width - this.petWareHouseModule.width) / 2 - 200;
            var _loc_3:* = (Global.stage.height - this.petWareHouseModule.height) / 2;
            this.petWareHouseModule.show(_loc_2, _loc_3);
            this.petMeetSelectPetWindowOpen();
            NpcEffectRule.registDialogEffectWindow(this.petWareHouseModule);
            
        }

        private function setPetOrder() : void
        {
            var _loc_1:* = cache.petWareHouse.capacity;
            var _loc_2:* = cache.petWareHouse.petBag.dictIntStr;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_1)
            {
                
                if (_loc_2[(_loc_3 + 1)] == null)
                {
                    this._petOrder = _loc_3 + 1;
                    break;
                }
                _loc_3++;
            }
            
        }

        private function petMeetSelectPetWindowOpen() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            _loc_1 = this.petWareHouseModule.x + this.petWareHouseModule.width;
            _loc_2 = this.petWareHouseModule.y + this.petWareHouseModule.height - this.petMeetSelectPetWindow.height;
            this.petMeetSelectPetWindow.updateDataProvider(cache.pet.petInfos);
            this.petMeetSelectPetWindow.show(_loc_1, _loc_2);
            NpcEffectRule.registDialogEffectWindow(this.petMeetSelectPetWindow);
            
        }

        private function onConfirmSelectedPetHandler(event:DataEvent) : void
        {
            var _loc_2:SPetInfo = null;
            if (this._petMeetSelectPetWindow != null && !this._petMeetSelectPetWindow.isHide)
            {
                if (!cache.petWareHouse.isPetBagFull() > 0)
                {
                    this._isStore = true;
                }
                _loc_2 = event.data as SPetInfo;
                if (_loc_2 != null)
                {
                    GameProxy.petProxy.storeWarehousePet(this._petOrder, _loc_2.uid);
                }
            }
            
        }

        private function onLookUpPetHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            GameProxy.petProxy.lookOverWarehousePet(_loc_2);
            
        }

        private function onTakeOutPetHandler(event:DataEvent) : void
        {
            var _loc_2:Object = null;
            var _loc_3:String = null;
            if (!cache.pet.isPetBagFull())
            {
                _loc_2 = event.data as Object;
                _loc_3 = _loc_2.petUid as String;
                this._petOrder = _loc_2.order as int;
                this._isTakeOut = true;
                GameProxy.petProxy.takeOutWarehousePet(_loc_3);
            }
            else
            {
                MsgManager.addBroadCast("宠物栏位已满，不能携带更多宠物");
            }
            
        }

        private function onStorePetHandler(event:DataEvent) : void
        {
            var _loc_2:GlowFilter = null;
            var _loc_3:Array = null;
            this._petOrder = event.data as int;
            if (this.petMeetSelectPetWindow.isHide)
            {
                this.petMeetSelectPetWindowOpen();
            }
            else
            {
                if (this._timer == null)
                {
                    this._timer = new Timer(200, 2);
                }
                this._timer.reset();
                this._timer.start();
                this._timer.addEventListener(TimerEvent.TIMER_COMPLETE, this.onTimerCompleteHandler);
                _loc_2 = new GlowFilter(16777062, 1, 3, 2, 3);
                _loc_3 = [_loc_2];
                EffectManager.glowFilterReg(this.petMeetSelectPetWindow, _loc_3, 0.6, 10, 5, 0);
            }
            
        }

        private function onTimerCompleteHandler(event:TimerEvent) : void
        {
            this._timer.stop();
            this._timer.removeEventListener(TimerEvent.TIMER_COMPLETE, this.onTimerCompleteHandler);
            this._timer = null;
            EffectManager.glowFilterUnReg(this.petMeetSelectPetWindow);
            
        }

        private function onExpendCapacityHandler(event:DataEvent) : void
        {
            GameProxy.petProxy.expandWarehouse();
            
        }

        private function onPetBagUpdateHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SPetUpdate;
            if (this._isStore)
            {
                this._isStore = false;
                this.petWareHouseModule.storePet(_loc_2.petInfo, this._petOrder);
            }
            else if (this._isTakeOut)
            {
                this._isTakeOut = false;
                this.petWareHouseModule.takeOutPet(_loc_2.petInfo, this._petOrder);
            }
            if (this._petMeetSelectPetWindow != null)
            {
                this._petMeetSelectPetWindow.updateDataProvider(cache.pet.petInfos);
            }
            
        }

        private function onWindowCloseHandler(event:DataEvent) : void
        {
            if (!this.petMeetSelectPetWindow.isHide)
            {
                this.petMeetSelectPetWindow.hide();
            }
            
        }

        private function onRecivePetInfoHandler(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SPetInfo;
            this.petViewWindow.updatePetInfoPanel(_loc_2);
            var _loc_3:* = this.petWareHouseModule.x + this.petWareHouseModule.width;
            var _loc_4:* = (Global.stage.height - this.petViewWindow.height) / 2;
            this.petViewWindow.show(_loc_3, _loc_4);
            NpcEffectRule.registDialogEffectWindow(this.petViewWindow);
            
        }

        private function onBagCapacityChangeHandler(param1:Object) : void
        {
            if (this._petWareHouseModule != null)
            {
            }
            
        }

        private function onWareHousePetChangeHandler(param1:Object) : void
        {
            this.setPetOrder();
            
        }

        private function checkOutDistance(event:PlayerEvent) : void
        {
            var _loc_3:NPCInfo = null;
            var _loc_4:Point = null;
            var _loc_5:Number = NaN;
            var _loc_2:* = RolePlayer.instance.currentPoint;
            if (this._petWareHouseModule && !this._petWareHouseModule.isHide)
            {
                _loc_3 = cache.dialog.npcInfo;
                if (_loc_3)
                {
                    _loc_4 = GameMapUtil.getPixelPoint(_loc_3.snpc.point.x, _loc_3.snpc.point.y);
                    _loc_5 = Math.sqrt(Math.pow(_loc_4.x - _loc_2.x, 2) + Math.pow(_loc_4.y - _loc_2.y, 2));
                    if (_loc_5 > 150)
                    {
                        this._petWareHouseModule.hide();
                        if (this._petMeetSelectPetWindow && !this._petMeetSelectPetWindow.isHide)
                        {
                            this._petMeetSelectPetWindow.hide();
                        }
                    }
                }
            }
            
        }

    }
}
